package com.tevintao.marketingsystem.task;

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import java.util.Random;


public class FriendCommentTaskProcess extends SetTaskExtra {
    private static FriendCommentTaskProcess ourInstance = new FriendCommentTaskProcess();

    public static FriendCommentTaskProcess getInstance() {
        return ourInstance;
    }

    private FriendCommentTaskProcess() {
    }

    private int mSelectPosition = -1;
    private Random mRandom = new Random();

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {

        LogManager.getInstance().logInfo("BrowFriendTaskProcess processView contextName = " + contextName + " v = " + v.toString());
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            LogManager.getInstance().logInfo("intab = " + inDiscover);
            if (inDiscover) {
                View friendView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.DISCOVER_LIST_ITEM_ID, "朋友圈");
                LogManager.getInstance().logInfo("friendView = " + friendView);
                if (null != friendView) {
                    ViewOperation.clickView(friendView);
                }
            }
            mSelectPosition = -1;
        } else {
            if (WxUiViewConfig.CIRCLE_OF_FRIENDS.equals(contextName)) {

                //点击输入框，跳出来输入键盘，可以开始进行评论。如果检测到了发送按钮
                View buttonViewFaSong = v.findViewById(2131627671);
                if (null != buttonViewFaSong && buttonViewFaSong instanceof TextView) {
                    View editViewShuRu = v.findViewById(2131627669);
                    if (editViewShuRu instanceof EditText) {
                        String msg = null;
                        EditText textView = (EditText) editViewShuRu;
                        if ("".equals(textView.getText().toString())) {
                            //TextView textView = (TextView) buttonViewFaSong;
                            LogManager.getInstance().logInfo("等待键盘自动输入评论内容 ");
                            Object comment = getExtra(WxUiViewConfig.KEY_CHAT_CONTENT);
                            if (null != comment) {
                                msg = comment.toString();
                            } else {
                                String[] str = new String[]{
                                        "今天天气真好", "好文章", "嗨，好久不见了啊", "最近生活不错啊", "毕业了吧", "现在在哪里工作呢", "粒粒皆辛苦", "谁知盘中餐", "锄禾日当午", "汗滴禾下土"
                                };
                                msg = str[mRandom.nextInt(10)];
                            }
                            ViewOperation.setText(textView, msg);
                            LogManager.getInstance().logInfo("评论成功 评论内容 = " + msg);
                        } else {
                            TextView textViewFaSong = (TextView) buttonViewFaSong;
                            if ("发送".equals(textViewFaSong.getText().toString())) {
                                ViewOperation.clickView(buttonViewFaSong);
                                taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "输入评论之后，按下了发送按钮");
                            }
                        }
                    }

                } else {
                    View listView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_LIST_VIEW);
                    LogManager.getInstance().logInfo("listView = " + listView);
                    if (null != listView && listView instanceof ListView) {
                        ListView list = (ListView) listView;
                        if(list.getAdapter().getCount()==2)
                            taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "当前微信号 里  还没有朋友圈");
                        if(list.getAdapter().getCount()>=3) {
                            if (mSelectPosition < 0) {
                                //mSelectPosition = mRandom.nextInt(list.getAdapter().getCount() - 1) + list.getHeaderViewsCount() + 1;
                                mSelectPosition = mRandom.nextInt(list.getAdapter().getCount() - 2) + list.getHeaderViewsCount();
                            }
                        int first = list.getFirstVisiblePosition();
                        int last = list.getLastVisiblePosition();
                        LogManager.getInstance().logInfo("mSelectPosition < 0 mSelectPosition = " + mSelectPosition + " first = " + first + " last =  " + last + " headerCount = " + list.getHeaderViewsCount() + " headerCount = " + list.getFooterViewsCount() + " total = " + list.getAdapter().getCount());
                        if ((mSelectPosition==1&&last==1)||(mSelectPosition < first || mSelectPosition > last)) {
                            //ViewOperation.moveListPosition(list, --mSelectPosition);
                            ViewOperation.moveListPosition(list, mSelectPosition);
                            LogManager.getInstance().logInfo("不到位");
                        } else {
                            LogManager.getInstance().logInfo("移动到位");
                            View  firstView=null;
                            if(first==0)
                                firstView = list.getChildAt(1);
                            else if(mSelectPosition==list.getAdapter().getCount()-2)
                                firstView = list.getChildAt(last-first-1);
                            else
                                firstView = list.getChildAt(0);
                            if (null != firstView) {
                                View agreeView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_COMMENT);
                                LogManager.getInstance().logInfo("agreeView = " + agreeView);
                                if (null != agreeView && agreeView.getVisibility() == View.VISIBLE) {
                                    TextView textView = (TextView) agreeView;
                                    if ("评论".equals(textView.getText().toString())) {
                                        ViewOperation.clickView(agreeView);
                                        // taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "打开评论");
                                        LogManager.getInstance().logInfo("打开评论 ");

                                        //JSONObject joTree = HookWindowManagerGlobal.GetViewJsonTree((ViewGroup) v, -1);
                                        // HookWindowManagerGlobal.printViewTreeJson(joTree, 0);
                                    }
                                } else {
                                    View menuView = firstView.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_OPEN_MENU);
                                    if (null != menuView) {
                                        ViewOperation.clickView(menuView);
                                    }
                                    LogManager.getInstance().logInfo("menuView=" + menuView);
                                }
                            }
                          }
                        }
                    }
                }
            }
        }
        return false;
    }
}
